﻿using System;
using System.Collections;
using System.Reflection;
using log4net;
using log4net.Config;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
using System.ServiceModel;
using System.ComponentModel;
using System.ServiceModel.Security;
using System.Collections.Generic;
using System.ServiceModel.Description;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;


namespace HaiFang
{
    [Serializable]
    public class ProxyMenuInfo
    {
        private static string PROCEDURE_QueryMenuInfo = "QueryMenuInfo";
        private static string PROCEDURE_QueryModuleSubInfoMenu = "QueryModuleSubInfoMenu";

        //@CurrUserID			int,					--管理员编号
        //@ProjectID			bigint
        public static IList<MenuInfo> QueryMenuInfo(int CurrUserID, int ProjectID)
        {
            IList<MenuInfo> list = new List<MenuInfo>();
            try
            {
                using (SqlHelper helper = new SqlHelper())
                {
                    #region 构造参数
                    string comandText = PROCEDURE_QueryMenuInfo;
                    List<SqlParameter> Parameters = new List<SqlParameter>();
                    Parameters.Add((new SqlParameter("@CurrUserID", CurrUserID)));
                    Parameters.Add(new SqlParameter("@ProjectID", ProjectID));
                    #endregion

                    #region 获取执行结果
                    DataTable dt = helper.ExecuteDataTable(comandText, CommandType.StoredProcedure, Parameters);
                    list = helper.GetObjects<MenuInfo>(dt);
                    #endregion
                }
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
            return list;
        }

        //@CurrUserID			int = null,					--管理员编号
        //@MenuID				bigint
        public static IList<MenuSubInfo> QueryModuleSubInfoMenu(int CurrUserID, int MenuID)
        {
            IList<MenuSubInfo> list = new List<MenuSubInfo>();
            try
            {
                using (SqlHelper helper = new SqlHelper())
                {
                    #region 构造参数
                    string comandText = PROCEDURE_QueryModuleSubInfoMenu;
                    List<SqlParameter> Parameters = new List<SqlParameter>();
                    Parameters.Add((new SqlParameter("@CurrUserID", CurrUserID)));
                    Parameters.Add(new SqlParameter("@MenuID", MenuID));
                    #endregion

                    #region 获取执行结果
                    DataTable dt = helper.ExecuteDataTable(comandText, CommandType.StoredProcedure, Parameters);
                    list = helper.GetObjects<MenuSubInfo>(dt);
                    #endregion
                }
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
            return list;
        }
   }

    [Serializable]
    public class MenuInfo
    {
        public int MenuId { get; set; }
        public int PrevMenuId { get; set; }
        public int MenuOrder { get; set; }
        public string MenuName { get; set; }
        public string MenuUrl { get; set; }
        public string MenuParam { get; set; }
        public int MenuLevel { get; set; }
        public string Description { get; set; }
        public int RightFlag { get; set; }
    }

    [Serializable]
    public class MenuSubInfo
    {
        public int AutoID { get; set; }
        public string GUID { get; set; }
        public int ModuleID { get; set; }
        public string ModuleSubName { get; set; }
        public DateTime CreateTime { get; set; }
        public string Description { get; set; }
        public int DeleteFlag { get; set; }
    }

    [Serializable]
    public class MenuFullInfo:MenuInfo
    {
        public IList<MenuSubInfo> MenuSubInfo { get; set; }

        public static MenuFullInfo Clone(MenuInfo mi)
        {
            MenuFullInfo mfi = null;
            if (mi != null)
            {
                mfi= new MenuFullInfo();
                mfi.MenuId=mi.MenuId;
                mfi.PrevMenuId=mi.PrevMenuId;
                mfi.MenuOrder=mi.MenuOrder;
                mfi.MenuName=mi.MenuName;
                mfi.MenuUrl=mi.MenuUrl;
                mfi.MenuParam=mi.MenuParam;
                mfi.MenuLevel=mi.MenuLevel;
                mfi.Description=mi.Description;
                mfi.RightFlag = mi.RightFlag;
            }
            return mfi;
        }

        public static IList<MenuFullInfo> QueryMenuFullInfo(int CurrUserID, int ProjectID)
        {
            IList<MenuFullInfo> list = new List<MenuFullInfo>();
            try
            {
                IList<MenuInfo> mis = ProxyMenuInfo.QueryMenuInfo(CurrUserID, ProjectID);

                bool fModule = false;
                MenuFullInfo fInfo = null;
                if (mis != null)
                {
                    for (int i = 0; i < mis.Count; i++)
                    {
                        if (mis[i].RightFlag > 0)
                        {
                            MenuFullInfo mfi = MenuFullInfo.Clone(mis[i]);
                            if (mfi.MenuLevel > 1)
                            {
                                //处理2级菜单
                                mfi.MenuSubInfo = ProxyMenuInfo.QueryModuleSubInfoMenu(CurrUserID, mfi.MenuId);
                                fModule = true;
                            }
                            list.Add(mfi);
                        }
                        else
                        {
                            //特殊处理功能模块
                            if (mis[i].MenuName == "功能模块")
                            {
                                fInfo = MenuFullInfo.Clone(mis[i]);
                            }
                        }
                    }
                }

                if (fInfo != null && fModule)
                {
                    list.Add(fInfo); 
                }
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
            return list;
        }
    }
}